#! /usr/bin/env python
#coding=utf-8

import urllib2
import lxml.html as HTML
from lxml import etree

from lxml.html import parse
import re
qurl = 'http://www.nuoqiu.com/column/1.html'
the_page  = urllib2.urlopen(qurl).read().decode("utf-8")
f = file('aa.txt','a')
def fromYahooStock(stoNum):
        qurl = 'http://www.nuoqiu.com/column/1.html'
        the_page  = urllib2.urlopen(qurl).read().decode("utf-8")
        root = HTML.document_fromstring(the_page) 
        #temp = root.xpath('//div[@class="gxshow"]//following-sibling::ul[1]//text()')
        #temp = root.xpath('//div[@class="gxshow"]//following-sibling::ul[1]')
#        nodes=temp[0].xpath("li/a")
#        #str =  [t.encode("UTF-8") for t in nodes]
#        for n in nodes:
#            #str =  [t.encode("UTF-8") for t in n]
#            #print n.text.encode("UTF-8")
#            print dir(n)
#            print n.attrib['href']
#            f.write(n.text.encode("UTF-8")+'\n')
#        f.close
#        str =  [t.encode("UTF-8") for t in temp]
#        for t in str:
#            f.write(t+'\n')
#            #print t
#        f.close
        
        print dir(temp)
def getpagecount(root):
    node = root.xpath('//div[@class="pageviewp"]//li[1]/text()')
    print node[0].encode("utf-8")
    b = re.compile(r'^\d{1}/(\d+)')
    d = b.search(node[0].encode("utf-8")).groups()[0]
    print d
    
    return str(node)

def getstorylist(root):
#    firstnode = temp = root.xpath('//div[@class="gxshow"]//ul[1]//text()')
#    remainnode = root.xpath('//div[@class="gxshow"]//following-sibling::ul[1]//text()')
#    nodes = firstnode + remainnode
#    str =  [t.encode("UTF-8") for t in nodes]
#    for t in str:
#        f.write(t+'\n')
#        print t
#    f.close
     firstnode = temp = root.xpath('//div[@class="gxshow"]//ul[1]')
     remainnode = root.xpath('//div[@class="gxshow"]//following-sibling::ul[1]')
     
     out = {}
     for node in (firstnode + remainnode):
        list = node.xpath("li")
        #print list
        out['dic_href'] = list[0].xpath("a")[0].attrib['href']
        out['name'] = list[0].xpath("a")[0].text.encode('utf-8')
        out['author'] = list[2].text.encode('utf-8')
        out['updatetime'] = list[3].text.encode('utf-8')
        #out['is_over'] = list[4].text.encode('utf-8')
        #print list[4].text
        for field in out:
            f.write(out[field] + '   ')
        f.write("\n")
        #print out
#        for author in list:
#            print author.text.encode("utf-8")
#            f.write(author.text.encode('utf-8')+'\n')

     f.close
     
    

if __name__ == "__main__":
    root = HTML.document_fromstring(the_page)
    getpagecount(root)
    getstorylist(root)
    #fromYahooStock(1201)   

